package com.douglas.managesystem.service.student.impl;

import com.douglas.managesystem.common.SearchEntity;
import com.douglas.managesystem.dao.student.LessonMapper;
import com.douglas.managesystem.entity.studentpojo.Lesson;
import com.douglas.managesystem.service.student.LessonService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;

/**
 * @Description 课程业务实现
 * @Author 以梦为馬 <Douglas1314@126.com>
 * @Version V1.0.0
 * @Date 2020-11-16 22:35
 **/
@SuppressWarnings("all")
@Service
public class LessonServiceImpl implements LessonService {


    @Autowired
    private LessonMapper lessonMapper;


    /**
     * 1.分页查询所有课程
     * @param params
     * @return
     */
    @Override
    public PageInfo<Lesson> findAllStuByPage(SearchEntity params) {
        if(params.getPageNum() == null || params.getPageNum() == 0) {
            params.setPageNum(1);
        }
        if(params.getPageSize() == null || params.getPageSize() == 0) {
            params.setPageSize(10);
        }
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        List<Lesson> lessons = lessonMapper.queryListByPage(params);

        PageInfo<Lesson> page = new PageInfo<Lesson>(lessons);
        return page;
    }

    /**
     * 2.新增科目
     * @param lesson
     */
    @Override
    public void saveLesson(Lesson lesson) {
        //1.先查询数据库是否存在，不进行新增
        Lesson lessDb = lessonMapper.queryLessonByLessonId(lesson.getLessonId());
        //2.判断返回数据
        if(lessDb != null){
            return;
        }
        //3.执行新增
        lessonMapper.insert(lesson);
    }

    /**
     * 3.根据id查询科目对象
     * @param id
     * @return
     */
    @Override
    public Lesson findLessonById(String id) {
        Lesson lesson = lessonMapper.queryLessonById(id);
        return lesson;
    }

    /**
     * 4.更新课程对象
     * @param lesson
     */
    @Override
    public void updateLesson(Lesson lesson) {
        lessonMapper.update(lesson);
    }

    /**
     * 5.删除
     * @param id
     */
    @Override
    public void delete(String id) {
        lessonMapper.delete(id);
    }

    /**
     * 6.查询 所有科目id的集合
     * @return
     */
    @Override
    public List<String> queryAllLessList() {
        return lessonMapper.queryAllLessonList();
    }
}
